# load packages
library(tidyverse)
library(ggplot2)
library(readxl)
library(dplyr)
library(scales)

# set wd
setwd('/Users/celinascott-buechler/DFP/dac_survey/Replication/Analysis/')

#dac knowledge before survey
survey <- read_csv('National Survey/Data/dac_cleaned_final.csv', col_names = TRUE)
notheard <- filter(survey, BaselineHeardDAC == "Nothing at all")
length(notheard$Progress)
heard <- filter(survey, BaselineHeardDAC != "Nothing at all")
length(heard$Progress)
1 - (450/(450+1183))

#figure 1
figure1 <- read_excel('Focus Groups/focus-group-pre-post-surveys_all.xlsx')
#View(figure1)



figure1_3 <- data.frame(figure1$`3. If you marked “a lot”, “some”, or “only a little”  for question 2, would you overall approve or disapprove of a direct air capture project being built in or near your community?`)

colnames(figure1_3)[1] ="post_Q3"

figure1_3$post_Q3 <- factor(figure1_3$post_Q3, levels = c("N/A",
                                                    "Strongly disapprove",
                                                    "Somewhat disapprove",
                                                    "Neither approve nor disapprove",
                                                    "Somewhat approve",
                                                    "Strongly approve"))

tab <- as.data.frame(table(figure1_3))
tab$perc <- tab$Freq/sum(tab$Freq)


ggplot(tab, aes(x = post_Q3, y = perc)) +
  geom_bar(stat = "identity", width = 0.7, fill = "#021F82") +
  geom_text(aes(label = scales::percent(perc, accuracy = 1), fontface = "bold"),
            vjust = -0.75) +
  scale_y_continuous(labels = scales::percent) +
  theme_minimal() +
  theme(axis.title.y = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title.x = element_blank())


figure1_8 <- figure1 %>% select(`8. After discussing direct air capture in your breakout group, how likely or unlikely are you to consider a direct air capture hub in your community if it follows your group’s project proposal?`)

colnames(figure1_8)[1] ="post_Q8"

figure1_8$post_Q8 <- factor(figure1_8$post_Q8, levels = c("Don't know",
                                                  "Very unlikely",
                                                  "Somewhat unlikely",
                                                  "Neither likely nor unlikely",
                                                  "Somewhat likely",
                                                  "Very likely"))

tab2 <- as.data.frame(table(figure1_8))
tab2$perc <- tab2$Freq/sum(tab2$Freq)

ggplot(tab2, aes(x = post_Q8, y = perc)) +
  geom_bar(stat = "identity", width = 0.7, fill = "#021F82") +
  geom_text(aes(label = scales::percent(perc, accuracy = 1), fontface = "bold"),
            vjust = -0.75) +
  scale_y_continuous(labels = scales::percent) +
  theme_minimal() +
  theme(axis.title.y = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title.x = element_blank())




#Figure 3
figure3 <- survey %>% select(PreConjointSupportInUS,
                     `PreConjointSupportNear Community`,
                     PreConjointSupportInCommunity, nationalweight)
#View(figure3)
figure3 <- pivot_longer(figure3, names_to = "question",
                           values_to = "answer",
                           1:3)
figure3$answer <-
  factor(figure3$answer, levels = c("Don't know", "Strongly oppose", "Somewhat oppose",
                                    "Somewhat support", "Strongly support"))
figure3$question <- factor(figure3$question, levels = c("PreConjointSupportInCommunity",
                                                              "PreConjointSupportNear Community",
                                                              "PreConjointSupportInUS"))
figure3 <- figure3 %>%
  count(answer = factor(answer), question = factor(question)) %>%
  mutate(pct = prop.table(n))

ggplot(figure3, aes(x = answer, y = pct, fill = question,
                    label = scales::percent(pct, accuracy = 1)),
       weight = nationalweight) +
  geom_bar(position = position_dodge(), width = 0.7, stat = 'identity') +
  geom_text(vjust = -0.75, position = position_dodge(width = .75), size = 5,
            aes(fontface = "bold")) +
  scale_y_continuous(labels = scales::percent) +
  scale_fill_manual(labels = c("PreConjointSupportInCommunity" =
                                 "Support siting DAC in their community",
                               "PreConjointSupportNear Community" =
                                 "Support siting DAC near their community",
                               "PreConjointSupportInUS" =
                                 "Support siting DAC in the U.S."),
                    guide = guide_legend(reverse = FALSE, title = ""),
                    values = c("PreConjointSupportInCommunity" = "#CEEBFF",
                               "PreConjointSupportNear Community" = "#6885E8",
                               "PreConjointSupportInUS" = "#021F82")) +
  theme_minimal() +
  theme(panel.grid.minor.y = element_blank(),
        axis.title.x = element_blank(),
        title = element_blank(),
        axis.text.x=element_text(size=14),
        axis.text.y = element_text(size = 14),
        strip.text.x = element_text(size = 14),
        legend.text = element_text(size = 14),
        legend.position = "bottom")


#Figure 4

figure4 <- survey %>% select(BaselineHeardSupportInCommunity,
                             PreConjointSupportInCommunity,
                             `BaselineHeardSupportNear Community`,
                             `PreConjointSupportNear Community`,
                             BaselineHeardSupportInUS,
                             PreConjointSupportInUS,
                             nationalweight)
figure4 <- figure4 %>% filter(BaselineHeardSupportInCommunity != "NA" |
                                `BaselineHeardSupportNear Community` != "NA" |
                                BaselineHeardSupportInUS != "NA")
figure4 <- pivot_longer(figure4, names_to = "question",
                        values_to = "answer",
                        1:6)

figure4$answer <- factor(figure3$answer, levels = c("Don't know",
                                                    "Strongly oppose",
                                                    "Somewhat oppose",
                                                    "Somewhat support",
                                                    "Strongly support"))
group <- data.frame('group'=c("Support DAC in their community",
                              "Support DAC near their community",
                              "Support DAC in the U.S.",
                              "Support DAC in their community",
                              "Support DAC near their community",
                              "Support DAC in the U.S."),
                    'q' = c("BaselineHeardSupportInCommunity",
                            "BaselineHeardSupportNear Community",
                            "BaselineHeardSupportInUS",
                            "PreConjointSupportInCommunity",
                            "PreConjointSupportNear Community",
                            "PreConjointSupportInUS"),
                    'time'= c("Before", "Before", "Before", "After", "After",
                              "After"))
#View(group)
figure4 <- left_join(figure4, group, by = c("question" = "q"))
figure4$time <- factor(figure4$time, levels = c("Before", "After"))
#View(figure3)
figure4 <- figure4 %>% group_by(group, time) %>%
  count(answer = factor(answer), question = factor(question)) %>%
  mutate(pct = prop.table(n))

ggplot(figure4, aes(x = answer, y = pct,
                    fill = time, label = scales::percent(pct, accuracy = 1)),
       weight = nationalweight) +
  geom_bar(position = position_dodge(), width = 0.7, stat = 'identity') +
  geom_text(vjust = -0.75, position = position_dodge(width = .75), size = 4,
            aes(fontface = "bold")) +
  theme_minimal() + facet_grid(.~group) +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels = c("Don't know" = "Don't\nknow",
                              "Strongly oppose" = "Strongly\noppose",
                              "Somewhat oppose" = "Somewhat\noppose",
                              "Somewhat support" = "Somewhat\nsupport",
                              "Strongly support" = "Strongly\nsupport")) +
  scale_fill_manual(labels = c("Before" = "Before", "After" = "After"),
                    guide = guide_legend(reverse = FALSE, title = ""),
                    values = c("Before"= "#5774BC",
                               "After"= "#021F82")) +
  theme(axis.title.x = element_blank(),
        axis.text.x=element_text(size=12.5),
        axis.text.y = element_text(size = 14),
        strip.text.x = element_text(size = 14),
        legend.text = element_text(size = 14),
        legend.position = "bottom",
        title = element_blank(),
        panel.grid.minor.y = element_blank())
